<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/7
 * Time: 14:35
 */

namespace app\admin\controller;


use think\Db;
use app\Model\AdModel;
use app\Model\PositionModel;
class Ad extends Common
{
    /**
     * 广告列表查询显示
     */
    public function adList(){
        $where =[];
        if (input('pid')){
            $where = "pid=".input('pid');
        }
        if(input('keywords')){
            $keywords = input('keywords');
            $where = "ad_name like '%$keywords%'";
        }

        $res = Db::name('tp_ad')
            ->where($where)
            ->order('pid desc')
            ->paginate(10);
       $ad_position = Db::name('tp_ad_position')
           ->alias('a')
           ->field('a.position_id,a.position_name,a.is_open')
           ->select();
        $ad_position_list = array();
       foreach ($ad_position as $k=>$v){
           $ad_position_list[$v['position_id']] = $v;
       }
        $this->getConAndAct('Ad');
        $this->assign('ad_position_list',$ad_position_list);
        $this->assign('res',$res);
        return view('adList');
    }

    /*
     * 新增广告  跳转页面
     */
    public function adAdd(){
        $ad_position = Db::name('tp_ad_position')
            ->alias('a')
            ->field('a.position_id,a.position_name,a.is_open')
            ->select();
        $ad_position_list = array();
        foreach ($ad_position as $k=>$v){
            $ad_position_list[$v['position_id']] = $v;
        }

        $this->assign('ad_position_list',$ad_position_list);
        return view('add');
    }

    /**
     * 新增页面图片上传返回地址
     * unlink 删除文件
     * is_dir 判断目录是否存在
     * mkdir  创建文件
     * move 保存文件至指定位置
     */
    public function upload()
    {
       if (!empty(input('code_back'))){
           $this->del_file(input('code_back'));
       }
        $file = request()->file('myfile');
        $path = ROOT_PATH.'public/upload/ad';
        if (!is_dir($path)){
            mkdir($path,0777,true);
        }
        $info = $file->move(ROOT_PATH . 'public/upload/ad');
        $address=$info->getSaveName();
        $imgp= str_replace("\\","/",$address);
        $imgpath='/upload/ad/'.$imgp;
        $banner_img= $imgpath;
        $response = array();
        if($info){
            $response['isSuccess'] = true;
            $response['imgpath'] = $imgpath;
        }else{
            $response['isSuccess'] = false;
            $response['error'] = '图片上传失败';
        }
        return $response;
    }


    /**
     * 广告新增方法
     */
    public function add(){
        $ad_link=input('ad_link');
        $ad_name = input('ad_name');
        if ($ad_link==''){
            $ad_link ='javascript:void(0);';
        }
        if ($ad_name==''){
            $ad_name = '自定义广告名称	';
        }

        $this->veri('Ad','add');
        $res = AdModel::create([
            'ad_name'=>$ad_name,
            'media_type'=>input('media_type'),
            'pid'=>input('pid'),
            'start_time'=>strtotime(input('start_time')),
            'end_time'=>strtotime(input('end_time')),
            'ad_link'=>$ad_link,
            'ad_code'=>input('ad_code'),
            'bgcolor'=>input('bgcolor'),
            'orderby'=>input('orderby')
        ]);
        if(!$res){
            return retJson('', 201, '新增失败');
        }

        return retJson('', 200, '新增成功');
    }

    /**
     * 是否新窗口打开改变方法
     */
    public function winopen(){
           $res = AdModel::update([
               'target'=>input('target')
           ],['ad_id'=>input('ad_id')]);
           return 0;
    }

    /**
     * 是否显示改变方法
     */
    public function showopen(){
        $res = AdModel::update([
            'enabled'=>input('enabled')
        ],['ad_id'=>input('ad_id')]);
        return 0;
    }

    /**
     * 排序改变方法
     */
    public function orderchange(){
        $order = input('orderby');
        if ($order == ''){
            $order = 0;
        }
       $res = AdModel::update([
            'orderby'=>$order
        ],['ad_id'=>input('ad_id')]);
        if (!$res){
            return [
                'code'=>200,
                'message'=>'修改失败'
            ];
        }
        return [
            'code'=>1,
            'message'=>'修改成功'
        ];
    }

    /**
     *修改广告 页面跳转
     */
    public function ad_edit(){
        $res = AdModel::get([
            'ad_id'=>input('ad_id')
        ]);
        $res['start_time'] = date("Y-m-d ", $res['start_time']);
        $res['end_time'] = date("Y-m-d ", $res['end_time']);
        $pname = Db::name('tp_ad_position')
            ->where('position_id',$res['pid'])
            ->field('tp_ad_position.position_name')
            ->find();
        $res['pname'] = $pname['position_name'];

        $ad_position = Db::name('tp_ad_position')
            ->alias('a')
            ->field('a.position_id,a.position_name,a.is_open')
            ->select();
        $ad_position_list = array();
        foreach ($ad_position as $k=>$v){
            $ad_position_list[$v['position_id']] = $v;
        }

        $this->assign('ad_position_list',$ad_position_list);
        $this->assign('res',$res);
        return view('edit');
    }

    /**
     * 广告修改方法
     */
    public function edit(){

        $this->veri('Ad','edit');
        $res = AdModel::update([
            'ad_name'=>input('ad_name'),
            'media_type'=>input('media_type'),
            'pid'=>input('pid'),
            'start_time'=>strtotime(input('start_time')),
            'end_time'=>strtotime(input('end_time')),
            'ad_link'=>input('ad_link'),
            'ad_code'=>input('ad_code'),
            'bgcolor'=>input('bgcolor'),
            'orderby'=>input('orderby')
        ],['ad_id'=>input('ad_id')]);
        if(!$res){
            return retJson('', 201, '修改失败');
        }

        return retJson('', 200, '修改成功');

    }

    /**
     * 广告删除方法
     */
    public function del(){

        $res = Db::name('tp_ad')->where('ad_id',input('ad_id'))->delete();
        if ($res == 1){
            if (file_exists('.'.input('ad_code'))){
                unlink('.'.input('ad_code'));
            }
            return [
                'code'=>1,
                'message'=>'删除成功'
            ];
        }else{
            return [
                'code'=>200,
                'message'=>'删除失败'
            ];
        }
    }

    /**
     * 广告位置列表查询显示
     */
    public function positionList(){
        $res = PositionModel::field('*')->order('position_id','desc')->paginate(10);
        $this->assign('res',$res);
        $this->getConAndAct('Ad');
        return view('position');
    }

    /**
     * 新增广告位置 跳转页面
     */
    public function positionadd(){
        return view('positionadd');
    }

    /**
     * 新增广告位置方法
     */
    public function addposition(){
        $this->veri('Position','positionadd');
        $res = PositionModel::create([
              'position_name' => input('position_name'),
              'ad_width' =>input('ad_width'),
              'ad_height' => input('ad_height'),
              'position_desc' => input('position_desc'),
              'is_open' => input('is_open')
        ]);
        if(!$res){
            return retJson('', 201, '新增失败');
        }

        return retJson('', 200, '新增成功');
    }

    /**
     * 广告位置状态改变方法
     */
    public function statechange(){
        $res = PositionModel::update([
            'is_open'=>input('is_open')
        ],['position_id'=>input('position_id')]);
        return 0;
    }

    /**
     * 编辑广告位置 页面跳转
     */
    public function position_edit(){
        $res = PositionModel::get([
            'position_id'=>input('position_id')
        ]);
        $this->assign('res',$res);
        return view('position_edit');



    }

    /**
     * 广告位置修改方法
     */
    public function position_upd(){
        $this->veri('Position','position_edit');
        $res = PositionModel::update([
            'position_name' => input('position_name'),
            'ad_width' =>input('ad_width'),
            'ad_height' => input('ad_height'),
            'position_desc' => input('position_desc'),
            'is_open' => input('is_open')
        ],['position_id'=>input('position_id')]);
        if(!$res){
            return retJson('', 201, '修改失败');
        }

        return retJson('', 200, '修改成功');
    }

}